package issues

import (
	"strconv"
	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/until"
	"time"

	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
)

type (
	Issues struct {
		ID                   bson.ObjectId `json:"_id" bson:"_id"`
		Group                string        `json:"group" bson:"group"`                               //组
		Title                string        `json:"title" bson:"title"`                               //标题
		ProductID            string        `json:"id" bson:"id"`                                     //款号
		IssuesDesc           string        `json:"issuesdesc" bson:"issuesdesc"`                     //描述
		AssignResolver       string        `json:"assignresolver" bson:"assignresolver"`             //解决人
		ExpectSettlementDate time.Time     `json:"expectsettlementdate" bson:"expectsettlementdate"` //期望日期
		SettlementDate       time.Time     `json:"settlementdate" bson:"settlementdate"`             //实际日期
		CounterMeasures      string        `json:"countermeasures" bson:"countermeasures"`           // 方案
		WhetherSolve         int           `json:"status" bson:"status"`                             //状态
		Creater              string        `json:"creater" bson:"creater"`                           //提出人
		CreaterDate          time.Time     `json:"createrdate" bson:"createrdate"`                   //创建日期
		Brand                string        `json:"brand" bson:"brand"`                               //品牌
	}
	InsertIssues struct {
		Group                string    `json:"group" bson:"group"`                               //组
		Title                string    `json:"title" bson:"title"`                               //标题
		ProductID            string    `json:"id" bson:"id"`                                     //款号
		IssuesDesc           string    `json:"issuesdesc" bson:"issuesdesc"`                     //描述
		AssignResolver       string    `json:"assignresolver" bson:"assignresolver"`             //解决人
		ExpectSettlementDate time.Time `json:"expectsettlementdate" bson:"expectsettlementdate"` //期望日期
		SettlementDate       time.Time `json:"settlementdate" bson:"settlementdate"`             //实际日期
		CounterMeasures      string    `json:"countermeasures" bson:"countermeasures"`           // 方案
		WhetherSolve         int       `json:"status" bson:"status"`                             //状态
		Creater              string    `json:"creater" bson:"creater"`                           //提出人
		CreaterDate          time.Time `json:"createrdate" bson:"createrdate"`                   //创建日期
		Brand                string    `json:"brand" bson:"brand"`                               //品牌
	}
)

type OXYIssuesData struct{}

func NewIssuesData() *OXYIssuesData {
	Data := &OXYIssuesData{}
	return Data
}

func (oi *OXYIssuesData) GetIssuesListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "issues"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []Issues{}
	query.All(&ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  ps,
		"count": count,
	})

	return nil
}

func (oi *OXYIssuesData) GetIssuesInfoHandler(c echo.Context) error {
	log.Info("GetUserInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "issues"
	query, err := model.DB().QueryOne(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p := Issues{}
	query.One(&p)
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
		"data": p,
	})

	return nil
}

func (oi *OXYIssuesData) AddIssuesHandler(c echo.Context) error {
	p := &InsertIssues{}
	if err := c.Bind(p); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyCms"
	model.DB().Document = "issues"
	err := model.DB().Insert(p)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (oi *OXYIssuesData) UpdateIssuesHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateProductIssuesHandler", id)

	p := &InsertIssues{}
	if err := c.Bind(p); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyCms"
	model.DB().Document = "issues"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, p)
	if err != nil {
		log.Error("更新出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (oi *OXYIssuesData) DelIssuesHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelProductIssuesHandler", id)
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "issues"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
